package org.swift.mashup.engine.concurrency.agenda;

import org.apache.log4j.Logger;
import org.swift.commons.client.ContextUtil;

/**
 * @author dhesson
 */
public class AgendaItem implements Runnable {
	private static final Logger log = Logger.getLogger(AgendaItem.class);
	
	private Runnable action;

	private String returnType;
	
	private Object context = null;
	private long startTime;
	private long endTime;

	public Object getContext() {
		return context;
	}

	public void setContext(Object context) {
		this.context = context;
	}

	/**
	 * 
	 * @param action
	 */
	public AgendaItem(Runnable action) {
		this.action = action;
	}

	/**
	 * 
	 * @param action
	 * @param returnType
	 */
	public AgendaItem(Runnable action, String returnType) {
		this.action = action;
		this.returnType = returnType;
	}


	/**
	 * Runs the given action and places the results of that action onto the
	 * working memory.
	 */
	public void run() {
		if (action == null) {
			// TODO: Throw a better exception here.
			throw new RuntimeException("No action has been specified.");
		}
		try {
			ContextUtil.setCurrentContext(this.context);
			startTime = System.currentTimeMillis();
			action.run();
		} catch (Exception exc) {
			throw new RuntimeException(exc);
		}
	}

	public Runnable getItem() {
		return action;
	}

	public String getReturnType() {
		return returnType;
	}
	
	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}
	
	public long getDuration(){
		return endTime - startTime;
	}
}
